package com.sfddata.musicpillow.user.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.sfddata.musicpillow.common.constant.BizCodeEnum;
import com.sfddata.musicpillow.common.respose.BaseRespose;
import com.sfddata.musicpillow.common.respose.EmptyObj;
import com.sfddata.musicpillow.user.entity.QuestionVo;
import com.sfddata.musicpillow.user.entity.SleepScoreTable;
import com.sfddata.musicpillow.user.entity.SleepScoreVo;
import com.sfddata.musicpillow.user.service.SleepScoreTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sfddata.musicpillow.user.entity.QuestionnaireInfoEntity;
import com.sfddata.musicpillow.user.service.QuestionnaireInfoService;
import com.sfddata.musicpillow.common.utils.PageUtils;
import com.sfddata.musicpillow.common.utils.R;



/**
 * 
 *
 * @author jyo
 * @email 820389501@qq.com
 * @date 2022-08-22 13:23:03
 */
@RestController
@RequestMapping("user/questionnaireinfo")
public class QuestionnaireInfoController {
    @Autowired
    private QuestionnaireInfoService questionnaireInfoService;
    @Autowired
    private SleepScoreTableService sleepScoreTableService;

    @GetMapping("/getHistoryQuestion")
    public BaseRespose<List<SleepScoreVo>> getHistoryQuestion(@RequestHeader String id){

        BaseRespose<List<SleepScoreVo>> respose = new BaseRespose<>();
        List<SleepScoreTable> list = sleepScoreTableService.getByUid(id);
        List<SleepScoreVo> sleepScoreVoList = new ArrayList<>();
        for (SleepScoreTable s : list
             ) {
            SleepScoreVo compare = sleepScoreTableService.compare(s);
            sleepScoreVoList.add(compare);
        }

        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        respose.setData(sleepScoreVoList);
        return respose;
    }

    @PostMapping("/getScore")
    public BaseRespose<SleepScoreVo> questionnaire(@RequestBody String mapString, @RequestHeader String id) throws ParseException {

        Map maps = (Map) JSON.parse(mapString);
        if ( maps.get("10").equals("8.0以上") ){
            maps.remove("10");
            maps.put("10", "8");
        }
        mapString = JSON.toJSONString(maps);

        if (!maps.containsKey("3")){
            maps.put("3", "null");
        }
        if (!maps.containsKey("40")){
            maps.put("40", "null");
        }
        if (!maps.containsKey("43")){
            maps.put("43", "null");
        }
        if (!maps.containsKey("44")){
            maps.put("44", "null");
        }
        if (!maps.containsKey("45")){
            maps.put("45", "null");
        }

        Integer PSQI = questionnaireInfoService.countHealthSleepScore(maps);
        Integer sleep ;
        if ( PSQI < 3 ){
            sleep = 41;
        }else if ( PSQI < 6 ){
            sleep = 32;
        }else if ( PSQI < 9 ){
            sleep = 28;
        }else if ( PSQI < 11 ){
            sleep = 23;
        }else if ( PSQI < 16 ){
            sleep = 14;
        }else if ( PSQI < 19 ){
            sleep = 8;
        }else{
            sleep = 3;
        }
        Integer CPSS = questionnaireInfoService.countPressScore(maps);
        Integer press;
        if ( CPSS < 6 ){
            press = 41;
        }else if ( CPSS < 11 ){
            press = 32;
        }else if ( CPSS < 15 ){
            press = 28;
        }else if ( CPSS < 20 ){
            press = 23;
        }else if ( CPSS < 26 ){
            press = 17;
        }else if ( CPSS < 31 ){
            press = 13;
        }else{
            press = 3;
        }
        Double eatTem = questionnaireInfoService.countEatingScore(maps);
        Double eat;
        if ( eatTem <= 0 ){
            eat = 0.5;
        }else if ( eatTem <= 8 ){
            eat = 1d;
        }else if ( eatTem <= 11 ){
            eat = 1.5;
        }else if ( eatTem <= 13 ){
            eat = 3d;
        }else if ( CPSS <= 15 ){
            eat = 4.5;
        }else{
            eat = 6d;
        }
        Double sportTem = questionnaireInfoService.countSportScore(maps);
        Double sport;
        if ( sportTem < 5 ){
            sport = 3d;
        }else if ( sportTem < 9 ){
            sport = 4.5;
        }else{
            sport = 6d;
        }
        Integer bodyTem = questionnaireInfoService.countBodyScore(maps);
        Double body;
        if ( bodyTem <= 3 ){
            body = 1d;
        }else if ( bodyTem < 6 ){
            body = 1.5;
        }else if ( bodyTem <= 7 ){
            body = 3d;
        }else if ( bodyTem <= 8 ){
            body = 4.5;
        }else{
            body = 6d;
        }


        List<String> showList = questionnaireInfoService.countShowlist(PSQI, CPSS, maps);

        SleepScoreTable sleepScoreTable = new SleepScoreTable(Integer.valueOf(id), sleep, press, eat, sport, body,
                eat + sport + body,
                sleep + press + eat + sport + body,
                maps.get("1").toString(), mapString, showList);

        sleepScoreTableService.save(sleepScoreTable);
        SleepScoreVo ssv = sleepScoreTableService.compare(sleepScoreTable);
        ssv.setShowList(showList);
        BaseRespose<SleepScoreVo> respose = new BaseRespose<>();
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        respose.setData(ssv);
        System.out.println(mapString+"111111111");
        System.out.println(showList+"22222222");
        return respose;

    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:questionnaireinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionnaireInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("user:questionnaireinfo:info")
    public R info(@PathVariable("id") Integer id){
		QuestionnaireInfoEntity questionnaireInfo = questionnaireInfoService.getById(id);

        return R.ok().put("questionnaireInfo", questionnaireInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:questionnaireinfo:save")
    public BaseRespose<EmptyObj> save(@RequestBody QuestionnaireInfoEntity questionnaireInfo){
        BaseRespose<EmptyObj> respose = new BaseRespose<>();

        try {
            questionnaireInfoService.save(questionnaireInfo);

        }catch (Exception e){
            respose.setCode(BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getCode());
            respose.setMsg(e.getMessage());
            return respose;
        }
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:questionnaireinfo:update")
    public R update(@RequestBody QuestionnaireInfoEntity questionnaireInfo){
		questionnaireInfoService.updateById(questionnaireInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:questionnaireinfo:delete")
    public R delete(@RequestBody Integer[] ids){
		questionnaireInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
